﻿using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Windows.Forms;
using Sync.AssignmentService;
using Sync.Log;
using Sync.SessionService;
using Sync.UserServices;
using Course = Sync.UserServices.Course;

namespace Sync.LMS
{
    public static class LMSServiceUsage
    {

        public static bool DoneTickling { get; set; }
        public static string MySessionKey { get; private set; }
        public static string Name { get; private set; }


        /// <summary>
        /// Logins to the LMS based off the provided Username and Password. Throws CommunicationException.
        /// </summary>
        /// <param name="username">User</param>
        /// <param name="password">Password</param>
        public static void LogIn(string username, string password)
        {
            using (var client = new SessionServicesClient())
            {
                try
                {
                    var loginResponse = client.Login(new LoginRequest { Username = username, Password = password });
                    MySessionKey = loginResponse.CurrentUser.SessKey;
                    Name = loginResponse.CurrentUser.FirstName + " " + loginResponse.CurrentUser.LastName;
                }
                catch (CommunicationException ce)
                {
                    MessageBox.Show("Wrong Username or Password. Please try again", "Error Username/Password",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Logs out of the LMS by sending a Logout Request.
        /// </summary>
        /// <returns>Logout Response</returns>
        public static LogoutResponse LogOut()
        {
            using (var client = new SessionServicesClient())
            {
                return client.Logout(new LogoutRequest());
            }
        }

        

        public static ObservableCollection<Course> GetCourses()
        {
            using (var client = new UserServiceClient())
            {
                GetCoursesResponse coursesResponse = client.GetCourses(new GetCoursesRequest { SessionKey = MySessionKey });
                var enumerator = coursesResponse.MyCourses.GetEnumerator();
                var myCourses = new ObservableCollection<Course>();
                while(enumerator.MoveNext())
                {
                    myCourses.Add((Course)enumerator.Current);
                }
                return myCourses;
            }
        }

        public static ObservableCollection<Sync.AssignmentService.Course> GetAssignments()
        {
            using (var client = new AssignmentServiceClient())
            {
                var assignmentResponse = client.GetAssignments(new AssignmentRequest{SessionKey = MySessionKey});
                var enumerator = assignmentResponse.Courses.GetEnumerator();
                var myCourses = new ObservableCollection<Sync.AssignmentService.Course>();
                while(enumerator.MoveNext())
                {
                    myCourses.Add((Sync.AssignmentService.Course)enumerator.Current);
                }
                return myCourses;
            }
        }

        public static void UploadAssignment(int assignmentId)
        {
            using ( var client = new AssignmentServiceClient())
            {
                //client.Upload(new UploadRequest{AssignmentId = assignmentId, File = new FileTransfer{}});
            }
        }

        public static void DownloadAssignment(int assignmentID)
        {
            using ( var client = new AssignmentServiceClient())
            {
                //var response = client.Download(new DownloadRequest
                //                    {
                //                        SessionKey = LMSServiceUsage.MySessionKey,
                //                        SubmissionId = assignmentID,
                //                    });
                //var browse = new FolderBrowserDialog();
                //browse.ShowDialog();
                //string FilePath = browse.SelectedPath;
                //if (!String.IsNullOrEmpty(FilePath))
                //{
                //    System.IO.File.WriteAllBytes(FilePath, (byte[]) (Array) response.dataField);
                //}
            }
        }
    }

    //Threaded class for starting the tickles
    public class Tickler
    {
        public void StartTickles()
        {
            //FileLog.LogMessage("Thread Started");
            //while (!LMSServiceUsage.DoneTickling)
            //{
            //    FileLog.LogMessage("Tickle sent");
            //    using (var client = new SessionServicesClient())
            //    {
            //        client.Tickle(new PingRequest());
            //    }
            //    Thread.Sleep(300000);
            //}
            //FileLog.LogMessage("Thread is now Closed.");
        }
    }
}
